package com.briup.estore.web.servlet;

import com.briup.estore.bean.Book;
import com.briup.estore.bean.Customer;
import com.briup.estore.bean.ShopAddress;
import com.briup.estore.service.BookService;
import com.briup.estore.service.ShopAddressService;
import com.briup.estore.service.impl.BookServiceImpl;
import com.briup.estore.service.impl.ShopAddressServiceImpl;
import com.briup.estore.vm.BookVM1;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/confirmOrder")
public class ConfirmOrderServlet extends HttpServlet {
    private BookService bookService;
    private ShopAddressService shopAddressService;

    {
        bookService = new BookServiceImpl();
        shopAddressService = new ShopAddressServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取书籍id和num之间的关系
        String str = req.getParameter("bookIdAndNum");
        // 处理数据
        String[] bookIdAndNum = {};
        if (str != null && !"".equals(str)) {
            bookIdAndNum = str.split(",");
        }

        // 根据书籍id查询书籍信息
        // 准备一个纯书籍的列表
        List<BookVM1> bookList = new ArrayList<>();
        for (String s : bookIdAndNum) {
            Book book = bookService.findBookById(Integer.parseInt(s.split("-")[0]));
            // 放入对象
            BookVM1 bookVM1 = new BookVM1();
            bookVM1.setId(book.getId());
            bookVM1.setName(book.getName());
            bookVM1.setAuthor(book.getAuthor());
            bookVM1.setPrice(book.getPrice());
            bookVM1.setCategoryId(book.getCategoryId());
            bookVM1.setImage(book.getImage());
            bookVM1.setPublisher(book.getPublisher());
            bookVM1.setPubdate(book.getPubdate());
            bookVM1.setDescription(book.getDescription());
            int i1 = Integer.parseInt(s.split("-")[1]);
            bookVM1.setNum(i1);
            bookVM1.setTotal(new BigDecimal(i1).multiply(new BigDecimal(bookVM1.getPrice()).setScale(2)).doubleValue());
            bookList.add(bookVM1);
        }
        HttpSession session = req.getSession();
        // 查询所有地址信息(用户id可以直接从session里取)
        Integer userId = -1;
        // 先从session里取
        Customer user = (Customer) session.getAttribute("user");
        // 如果session里没有，从cookie里取
        if (user == null) {
            Cookie[] cookies = req.getCookies();
            for (Cookie c : cookies) {
                if ("userId".equals(c.getName())) {
                    userId = Integer.parseInt(c.getValue());
                }
            }
        } else {
            userId = user.getId();
        }
        List<ShopAddress> addressList = shopAddressService.findAllByUserId(userId);

        // 存入session中并做跳转
        session.setAttribute("bookVmList", bookList);
        session.setAttribute("addressList", addressList);
        resp.sendRedirect("confirm");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
